package com.zzw.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzw.bean.Msg;
import com.zzw.bean.SysDictType;
import com.zzw.service.SysDictTypeService;
import com.zzw.utils.RedisUtil;
import com.zzw.utils.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典类型控制器
 * 提供字典类型的增删改查功能
 */
@Controller
@RequestMapping("sysDictType")
@Api(tags = "字典类型管理接口")
public class SysDictTypeController {

    @Resource
    private SysDictTypeService sysDictTypeService;

    @ApiOperation(value = "字典类型列表页面", notes = "跳转到字典类型列表页面")
    @RequestMapping("list")
    public String list() {
        return "dict/list";
    }

    @ApiOperation(value = "获取字典类型数据", notes = "分页获取字典类型数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, dataType = "Integer", paramType = "query", defaultValue = "1"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer", paramType = "query", defaultValue = "10"),
        @ApiImplicitParam(name = "sort", value = "排序字段", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序方式", required = false, dataType = "String", paramType = "query")
    })
    @RequestMapping("getData")
    @ResponseBody
    public Msg getData(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "sort", defaultValue = "") String sort,
                       @RequestParam(value = "order", defaultValue = "") String order,
                       SysDictType sysDictType) {
        PageHelper.startPage(pageNum, pageSize, WebUtils.getOrderBy(sort, order));
        List<SysDictType> list = sysDictTypeService.listByCondition(sysDictType);
        PageInfo pageInfo = new PageInfo(list, pageSize);
        return Msg.success().add("pageInfo", pageInfo);
    }

    @ApiOperation(value = "保存字典类型", notes = "新增字典类型信息")
    @RequestMapping("save")
    @ResponseBody
    public Msg save(SysDictType sysDictType) {
        return Msg.success(sysDictTypeService.save(sysDictType) > 0);
    }

    @ApiOperation(value = "更新字典类型", notes = "更新字典类型信息")
    @RequestMapping("update")
    @ResponseBody
    public Msg update(SysDictType sysDictType) {
        return Msg.success(sysDictTypeService.update(sysDictType) > 0);
    }

    @ApiOperation(value = "删除字典类型", notes = "根据ID删除字典类型")
    @ApiImplicitParam(name = "dicId", value = "字典类型ID", required = true, dataType = "Integer", paramType = "query")
    @RequestMapping("delete")
    @ResponseBody
    public Msg delete(Integer dicId) {
        return Msg.success(sysDictTypeService.delete(dicId) > 0);
    }
}
